package com.example.forum.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.forum.commons.tools.StringUtil;
import com.example.forum.entity.TAppuser;
import com.example.forum.entity.TTheme;
import com.example.forum.entity.ThemereplyModel;
import com.example.forum.service.ITAppuserService;
import com.example.forum.service.ITThemeService;
import com.example.forum.service.ITThemereplyService;
import com.example.forum.vo.JsonResult;
import com.example.forum.vo.PageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author why
 * @since 2020-04-22
 */
@RestController
@RequestMapping("ThemeReply")
public class TThemereplyController extends BaseController {

    @Autowired
    ITThemereplyService themereplyService;

    @Autowired
    ITAppuserService appuserService;

    @Autowired
    ITThemeService themeService;

    @GetMapping("GetThemesReplyByThemeId")
    public JsonResult getThemesReplyByThemeId(@RequestParam(defaultValue = "1") Integer pageIndex,
                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                              String themeId,
                                              @RequestParam(defaultValue = "true") boolean isDesc) {
        if (StringUtil.isEmpty(themeId)) {
            return new JsonResult(HttpStatus.NOT_FOUND, "文章id不能为空", null);
        }
        TAppuser appuser = appuserService.getById(currentUserId());
        if (appuser == null) {
            return new JsonResult(HttpStatus.NOT_FOUND, "用户不存在", null);
        }
        PageDto pageDto = themereplyService.getThemesReplyByThemeId((pageIndex - 1) * pageSize, pageSize, themeId, appuser.getId(), isDesc);
        return new JsonResult(HttpStatus.OK, "成功", pageDto);
    }

    /**
     * 评论
     *
     * @param themereplyModel
     * @return
     */
    @PostMapping("CreateThemeReply")
    public JsonResult createThemeReply(@RequestBody ThemereplyModel themereplyModel) {

        TAppuser appuser = appuserService.getById(currentUserId());
        if (appuser == null) {
            return new JsonResult(HttpStatus.NOT_FOUND, "用户不存在", null);
        }
        TAppuser one = appuserService.getOne(new QueryWrapper<TAppuser>()
                .eq("id", currentUserId())
                .eq("isProhibit", 1));
        if (one != null) {
            return new JsonResult(HttpStatus.NOT_FOUND, "用户已经禁用", null);
        }
        TTheme tTheme = themeService.getOne(new QueryWrapper<TTheme>()
                .eq("isDeleted", 0)
                .eq("id", themereplyModel.getTopicId()));
        if (tTheme==null) {
            return new JsonResult(HttpStatus.NOT_FOUND, "主题不存在", null);
        }
        themereplyService.createThemeReply(themereplyModel, appuser.getId());
        return new JsonResult(HttpStatus.OK, "成功", null);

    }

    @GetMapping("delete")
    public JsonResult delete(String themeReplyId,String type){
        return new JsonResult(HttpStatus.OK,"",null);
    }
}
